<?php

/*
*/

class Sale_Advice_model extends CI_Model
{

	static $TABLE;

	function __construct()
	{
		parent::__construct();
		//载入库文件
		$this->load->library('test_database');
		$this->test_database->test();
		//载入核心类
		$this->load->database($this->test_database->get_db());
		//载入库文件
		$this->load->library('form_validation');
		//载入本地语言文件
		$this->lang->load('form_validation','chinese');
		//初始化
		$this->TABLE = 'sale_advices';
		//临时表
		$this->TEMP_TABLE = 'temp_' . $this->TABLE;
		//模式
		$this->mode = FALSE;
	}

	/***
	For private function here
	***/
	/***
	For public function here
	***/

	//创建一个库存结算单条目
	public function create_advice($advice_id, $advice_date, $stock_details, $view=FALSE)
	{

		//载入需要的模型
		$this->load->model('stock_advices_model');
		$this->load->model('sale_advice_detail_model');

		$data = array(
			'advice_id'		=> $advice_id,
			'advice_date'	=> $advice_date,
			'create_date'	=> time()
			);
		if ($view)
		{
			//查看模式
		}
		else
		{
			//写入模式
			if ($this->db->insert($this->TABLE, $data))
			{
				$sale_advice_id = $this->db->insert_id();
			}
			else
			{
				$this->error = '创建销售结算报表失败';
				return FALSE;
			}
		}

		//获得成本库存结算单ID
		//$stock_advice_id = $this->stock_advices_model->get_id($advice_date);

		//创建销售结算报表详情条目
		if (FALSE === $this->sale_advice_detail_model->create_detail($sale_advice_id, $stock_details, $view))
		{
			//$this->error = __CLASS__ . '::' . __FUNCTION__ . ' ON LINE ' . __LINE__ . ':暂时无法结算，请稍候再试或联系管理员。' . $this->sale_advice_detail_model->error;
			$this->error = $this->sale_advice_detail_model->error;
			return FALSE;
		}
		return TRUE;
	}	

	//获得所有货物结算列表
	//返回一个包含所有值的列表
	public function get_list($limit=FALSE, $year=FALSE)
	{
		if ($limit)
		{
			$this->db->limit($limit);
		}
		if (!$year)
		{
			$this->db->order_by('sale_advice_id DESC');
			$query = $this->db->get($this->TABLE);
			if ($query)
			{
				return $query->result();
			}
			return array();
		}
	}

	/************
	获得具体的库存结算单数据
	$type[string] : 设置查看类型
	$value[string] : 查看类型对应的指定值
	$relate[boolean] : 判断是否关联最新的货物数据，还是直接使用结算中的货物信息数据
	************/
	public function get_one($type, $value, $relate=FALSE)
	{
		$this->load->model('sub_goods_model');
		$this->load->model('main_goods_model');
		$this->load->model('sale_advice_detail_model');
		$this->load->model('sale_advice_stock_model');

		$advice = array('general'=>array(), 'details'=>array());	//初始化返回数据容器

		switch ($type) {
			case 'ID':
				$this->db->where('sale_advice_id', $value);
				break;
			case 'DATE':
				$this->db->where('advice_date', $value);
				break;
			default:
				$this->error = '必须指定一个类型';
				break;
		}
		if ($this->error) return FALSE;
		$this->db->limit(1);
		$query = $this->db->get($this->TABLE);
		if (1 == $query->num_rows())
		{
			$advice['general'] = $query->row_array();
			$sale_advice_id = $advice['general']['sale_advice_id'];
			$advice['details'] = array();

			//取得库存结算详情数据
			$stock_advice_details = $this->sale_advice_detail_model->get_advice_details($sale_advice_id);

			//判断是否要关联最新数据
			if ($relate)
			{
				foreach ($stock_advice_details as $detail)
				{
					$main_goods_id = $detail->main_goods_id;
					$advice['details'][$main_goods_id]['info'] = $this->main_goods_model->getOne($main_goods_id);
					$advice['details'][$main_goods_id]['sub'][$detail->sub_goods_id] = array(
						'price'	=> $detail->sub_goods_price,
						'sale_quantity'		=> $detail->sub_goods_quantity,
						'return_quantity'	=> $detail->sub_goods_return_quantity,
						'stock'	=> $detail->quantity,
						'info'	=> $this->sub_goods_model->get_one($detail->sub_goods_id),
						);
				}
			}
			else
			{
				foreach ($stock_advice_details as $detail)
				{
					$main_goods_id = $detail->main_goods_id;
					$advice['details'][$main_goods_id]['info'] = array(
						'image'			=> $detail->image,
						'number'		=> $detail->number,
						'search_code'	=> $detail->search_code
						);
					$advice['details'][$main_goods_id]['sub'][$detail->sub_goods_id] = array(
						'price'	=> $detail->sub_goods_price,
						'sale_quantity'		=> $detail->sub_goods_quantity,
						'return_quantity'	=> $detail->sub_goods_return_quantity,
						'stock'	=> $detail->quantity,
						'info'	=> array(
							'image'		=> $detail->image,
							'attribute'	=> $detail->attribute
							)
						);
				}
			}
			return $advice;
		}
		else
		{
			$this->error = '此结算单不存在';
		}
		return FALSE;
	}

	//获得一次结算单的日期
	public function get_advice_date($type = 'last')
	{
		$date = FALSE;
		if ('last' == $type)
		{
			$this->db->order_by('advice_date DESC');
		}
		else if ('frist' == $type)
		{
			$this->db->order_by('advice_date ASC');
		}
		$this->db->select('advice_date');
		$this->db->limit(1);
		$date = $this->db->get($this->TABLE)->row();
		if (empty($date))
		{
			return FALSE;
		}
		else
		{
			return $date->advice_date;
		}
	}

	//返回偏移周期的对应的ID值
	public function get_offset_advice_id($sale_advice_id, $offset)
	{

		//判断列表是否没有记录
		if ($this->db->count_all_results($this->TABLE) < 1)
		{
			return 0;
		}

		if ($offset > 0)
		{
			$this->db->where('sale_advice_id > '.$sale_advice_id);
			$this->db->order_by('sale_advice_id DESC');
		}
		else if ($offset < 0)
		{
			$this->db->where('sale_advice_id < '.$sale_advice_id);
			$this->db->order_by('sale_advice_id ASC');
		}
		else
		{
			$this->db->where('sale_advice_id = '.$sale_advice_id);
			$this->db->limit(1);
			if ($this->db->count_all_results($this->TABLE) > 0)
			{
				return $sale_advice_id;
			}
			else
			{
				return -1;
			}
		}

		$offset = abs($offset);
		$this->db->limit($offset);
		$query = $this->db->get($this->TABLE);
		if ($query)
		{
			$result = $query->result();
			if (count($result) == $offset)
			{
				$sale_advice_id = array_shift($result);
				return $sale_advice_id->sale_advice_id;
			}
		}
		else
		{
			return 0;
		}
	}

	//获得所有库存阶段单
	public function get_all($select)
	{
		$this->db->select($select);
		$query = $this->db->get($this->TABLE);
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return 0;
		}
	} 

	//更新一个结算单信息
	public function update_row($row, $value, $sale_advice_id)
	{
		$data = array(
			$row 	=> $value
			);
		$this->db->where('sale_advice_id', $sale_advice_id);
		if ($this->db->update($this->TABLE, $data))
		{
			return TRUE;
		}
		else
		{
			$this->error = '更新错误';
			return FALSE;
		}
	}

	//更新一个结算单信息
	public function update_rows($data, $sale_advice_id)
	{
		$data = $data;
		$this->db->where('sale_advice_id', $sale_advice_id);
		if ($this->db->update($this->TABLE, $data))
		{
			return TRUE;
		}
		else
		{
			$this->error = '更新错误';
			return FALSE;
		}
	}

}